<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login_method">
        <button type="button" @click="changeToPwd">密码登录</button>
        <button type="button" @click="changeToCode">验证码登录</button>
      </div>
      <div v-show="isPwd">
        <el-form
          id="login_pwd"
          ref="pwdForm"
          :model="pwdForm"
          :rules="pwdLoginRules"
          label-width="70px"
        >
          <el-form-item label="邮箱" prop="email">
            <el-input v-model="pwdForm.email" class="pwd-item ml60" prefix-icon="el-icon-user" />
          </el-form-item>
          <el-form-item label="密码" prop="pwd">
            <el-input
              v-model="pwdForm.pwd"
              type="password"
              autocomplete="off"
              class="pwd-item ml60"
              prefix-icon="el-icon-lock"
            />
          </el-form-item>
          <div class="login-button">
            <button type="button" @click="loginByPwd">登录</button>
          </div>
        </el-form>
      </div>
      <div v-show="!isPwd">
        <el-form
          id="login_code"
          ref="codeForm"
          :model="codeForm"
          :rules="codeLoginRules"
          label-width="70px"
        >
          <div>
            <el-form-item label="邮箱" prop="email">
              <el-input v-model="codeForm.email" class="code-item ml60" prefix-icon="el-icon-user" />
              <el-button plain size="mini" class="ml20" @click="getCode">获取验证码</el-button>
            </el-form-item>
          </div>
          <div>
            <el-form-item label="验证码" prop="code">
              <el-input
                v-model="codeForm.code"
                type="text"
                class="code-item ml60"
                prefix-icon="el-icon-lock"
              />
            </el-form-item>
          </div>
          <div class="login-button">
            <button type="button" @click="loginByCode">登录</button>
          </div>
        </el-form>
      </div>
      <div class="ml100"><span>没有轻聘账号|</span><router-link to="/register/employer">立即注册</router-link></div>
    </div>
  </div>
</template>

<script>
import {
  getCode
} from '@/api/user'

export default {
  name: 'Login',
  data() {
    return {
      pwdForm: {
        email: '',
        pwd: ''
      },
      codeForm: {
        email: '',
        code: ''
      },
      isPwd: true,
      pwdLoginRules: {
        email: [
          { required: true, message: '请输入邮箱地址', trigger: 'blur' },
          { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] }
        ],
        pwd: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ]
      },
      codeLoginRules: {
        email: [
          { required: true, message: '请输入邮箱地址', trigger: 'blur' },
          { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] }
        ],
        code: [
          { required: true, message: '请输入验证码', trigger: 'blur' }
        ]
      }

    }
  },
  mounted() {
    document.querySelector('body').setAttribute('style', 'background-color:#F2F5F4')
  },
  beforeDestroy() {
    document.querySelector('body').removeAttribute('style')
  },
  methods: {
    loginByPwd() {
      this.$refs.pwdForm.validate((valid) => {
        if (valid) {
          const param = {
            email: this.pwdForm.email,
            password: this.pwdForm.pwd
          }
          this.$store
            .dispatch('user/loginByPwd', param)
            .then(
              () => {
                this.$router.push({
                  path: '/'
                })
              },
              (error) => {
                this.$message({
                  showClose: true,
                  message: error.message || '登录失败',
                  type: 'error'
                })
              }
            )
        }
      })
    },
    loginByCode() {
      // 获取邮箱和验证码
      this.$refs.codeForm.validate((valid) => {
        if (valid) {
          const param = {
            email: this.codeForm.email,
            code: this.codeForm.code
          }
          this.$store
            .dispatch('user/loginByCode', param)
            .then(
              () => {
                this.$router.push({
                  path: '/'
                })
              },
              (error) => {
                this.$message({
                  showClose: true,
                  message: error.message || '登录失败',
                  type: 'error'
                })
              }
            )
        }
      })
    },
    changeToPwd() {
      this.isPwd = true
    },
    changeToCode() {
      this.isPwd = false
    },
    getCode() {
      // 获取验证码
      const param = {
        email: this.codeForm.email
      }
      getCode(param).then(
        (response) => {
          this.$message({
            showClose: true,
            message: '邮件发送成功',
            type: 'success'
          })
        },
        (error) => {
          this.$message({
            showClose: true,
            message: error.message,
            type: 'error'
          })
        }
      )
    }
  }
}
</script>

<style lang="scss" scoped>
  .login-container {
    height: 100%;
    margin-top: 200px;
  }

  .login-box {
    width: 30%;
    margin: 0 auto;
    background: #ffffff;
    padding: 10px 40px 20px 40px;
  }

  .login_method {
    height: 55px;
  }

  .login_method button {
    width: 100px;
    height: 30px;
    line-height: 30px;
    float: left;
    font-size: 14px;
    border: 1px solid rgb(237, 224, 222);
    margin: 20px 40px;
    color: #414A60;
  }

  .login_method button:focus {
    outline: none;
    border: 1px solid #00b38a;
  }

  .login-button {
    margin: 20px auto;
  }

  .login-button button {
    width: 190px;
    height: 30px;
    font-size: 20px;
    font-weight: 700;
    color: #fff;
    background-image: linear-gradient(to right, #74ebd5 0%, #9face6 100%);
    border: 0;
    border-radius: 2px;
    margin-left: 100px;
  }

  .login-button button:hover {
    cursor: pointer;
  }

  .code-item {
    width: 210px;
  }

  .pwd-item {
    width: 300px;
  }

  .ml20 {
    margin-left: 2px;
  }

  .ml100 {
    margin-left: 110px;
  }
</style>
